package com.aeoj.clientmanagement.enums.problem;

import lombok.Getter;

/**
 * 题目提交状态枚举
 */
@Getter
public enum ProblemRrecordStatusEnum {
    PENDING("PENDING", "待判题"),
    JUDGING("JUDGING", "判题中"),
    SUCCESS("SUCCESS", "判题成功"),
    ERROR("ERROR", "判题失败"),
    ACCEPTED("ACCEPTED", "通过"),
    WRONG_ANSWER("WRONG_ANSWER", "答案错误"),
    COMPILE_ERROR("COMPILE_ERROR", "编译错误"),
    MEMORY_LIMIT_EXCEEDED("MEMORY_LIMIT_EXCEEDED", "内存溢出"),
    TIME_LIMIT_EXCEEDED("TIME_LIMIT_EXCEEDED", "超时"),
    PRESENTATION_ERROR("PRESENTATION_ERROR", "格式错误"),
    OUTPUT_LIMIT_EXCEEDED("OUTPUT_LIMIT_EXCEEDED", "输出溢出"),
    WAITING("WAITING", "等待中"),
    DANGEROUS_OPERATION("DANGEROUS_OPERATION", "危险操作"),
    RUNTIME_ERROR("RUNTIME_ERROR", "运行时错误"),
    SYSTEM_ERROR("SYSTEM_ERROR", "系统错误");

    private final String code;       // 状态码（英文标识）
    private final String description; // 状态描述（中文说明）

    ProblemRrecordStatusEnum(String code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 根据状态码获取枚举
     * @param code 状态码
     * @return 对应的枚举，找不到返回null
     */
    public static ProblemRrecordStatusEnum getByCode(String code) {
        if (code == null) {
            return null;
        }
        for (ProblemRrecordStatusEnum status : ProblemRrecordStatusEnum.values()) {
            if (status.getCode().equalsIgnoreCase(code)) {
                return status;
            }
        }
        return null;
    }

    /**
     * 检查状态码是否有效
     * @param code 状态码
     * @return 是否有效
     */
    public static boolean isValidCode(String code) {
        return getByCode(code) != null;
    }

    /**
     * 判断是否是成功状态（ACCEPTED）
     * @param code 状态码
     * @return 是否通过
     */
    public static boolean isAccepted(String code) {
        return ACCEPTED.getCode().equalsIgnoreCase(code);
    }

    /**
     * 判断是否是错误状态（非ACCEPTED和WAITING）
     * @param code 状态码
     * @return 是否是错误状态
     */
    public static boolean isErrorStatus(String code) {
        ProblemRrecordStatusEnum status = getByCode(code);
        return status != null && status != ACCEPTED && status != WAITING;
    }

    /**
     * 判断是否处于未完成状态（PENDING/JUDGING/WAITING）
     */
    public static boolean isProcessing(String code) {
        ProblemRrecordStatusEnum status = getByCode(code);
        return status == PENDING || status == JUDGING || status == WAITING;
    }

    /**
     * 判断是否可以取消判题（仅处理中状态可取消）
     */
    public static boolean isCancelable(String code) {
        return isProcessing(code);
    }
}